//package com.jike.mahjong.service.Imp;
//
//import com.jike.mahjong.common.Page;
//import com.jike.mahjong.common.util.PropertyUtil;
//import com.jike.mahjong.dao.MessageDao;
//import com.jike.mahjong.dao.ScheduleJobDao;
//import com.jike.mahjong.model.CronJob;
//import com.jike.mahjong.model.MessageModel;
//import com.jike.mahjong.model.Version;
//import com.jike.mahjong.service.JobService;
//import com.jike.mahjong.service.MessageService;
//import com.jike.mahjong.task.JobConstants;
//import com.jike.mahjong.task.MsgTask;
//import com.jike.mahjong.task.TaskUtil;
//import com.jike.mahjong.task.quartz.MsgSendTask;
//import org.apache.log4j.Logger;
//import org.quartz.*;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.quartz.SchedulerFactoryBean;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.Timer;
//
///**
// * Created by star on 2016/10/14.
// */
//@Service("jobService")
//public class JobServiceImp implements JobService {
//
//
//    private Logger log = Logger.getLogger(JobServiceImp.class);
//    @Autowired
//    private SchedulerFactoryBean schedulerFactoryBean;
//
//
//    @Autowired
//    private ScheduleJobDao jobDao;
//
//
//
//    @PostConstruct
//    public void init() throws Exception {
//
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        // 这里获取任务信息数据
//        List<CronJob> jobList = jobDao.getAll();
//
//        for (CronJob job : jobList) {
//            addJob(job);
//        }
//        log.debug("init success........");
//    }
//
//    public Page queryJobMessageInfo(Map<String, Object> para,Page page) {
//
//        int offset=(page.getCurrentPage()-1)*page.getPageSize();
//        para.put("offset",offset);
//        para.put("pageSize",page.getPageSize());
//        List<CronJob> jobList=jobDao.getAll();
//        int count=jobDao.countTaskNum();
//        page.setTotalCount(count);
//        page.setData(jobList);
//        return page;
//    }
//
//    @Override
//    public Integer insertMessageJob(CronJob job) {
//
//        Long count=new Date().getTime();
//        job.setJobName(JobConstants.MESSAGE_JOB_NAME+count);
//        job.setJobGroup(JobConstants.MESSAGE_JOB_GROUP);
//        String frequency="0/"+job.getFrequency()+" * * * * ?";
//
//        job.setCronExpression(frequency);
//        return jobDao.insertSelective(job);
//    }
//
//    @Override
//    public void changeStatus(Long jobId, String cmd) throws SchedulerException {
//        CronJob job = getTaskById(jobId);
//        if (job == null) {
//            return;
//        }
//        if ("stop".equals(cmd)) {
//            deleteJob(job);
//            job.setJobStatus(CronJob.STATUS_NOT_RUNNING);
//        } else if ("start".equals(cmd)) {
//            job.setJobStatus(CronJob.STATUS_RUNNING);
//            addJob(job);
//        }
//
//
//        jobDao.updateByPrimaryKeySelective(job);
//    }
//
//    /**
//     * 删除job
//     * @param jobId
//     * @return
//     * @throws SchedulerException
//     */
//    @Override
//    public Integer deleteMessageJob(Long jobId) throws SchedulerException {
//        int result=0;
//        CronJob job = getTaskById(jobId);
//        if (job == null) {
//            return 0;
//        }
//        deleteJob(job);
//
//        return jobDao.deleteByPrimaryKey(jobId);
//    }
//
//
//    /**
//     * 暂停一个job
//     *
//     * @param scheduleJob
//     * @throws SchedulerException
//     */
//    public void pauseJob(CronJob scheduleJob) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//        scheduler.pauseJob(jobKey);
//    }
//
//    /**
//     * 添加任务
//     *
//     * @throws SchedulerException
//     */
//    public void addJob(CronJob job) throws SchedulerException {
//        if (job == null || !CronJob.STATUS_RUNNING.equals(job.getJobStatus())) {
//            return;
//        }
//
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
//
//        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//
//        // 不存在，创建一个
//        if (null == trigger) {
//
//            JobDetail jobDetail = JobBuilder.newJob(MsgSendTask.class).withIdentity(job.getJobName(), job.getJobGroup()).usingJobData(JobConstants.JOB_DATA_KEY,job.getDescription()).build();
//
////            jobDetail.getJobDataMap().put("scheduleJob", job);
//
//            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//            trigger = TriggerBuilder.newTrigger().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
//
//            scheduler.scheduleJob(jobDetail, trigger);
//
//        } else {
//            // Trigger已存在，那么更新相应的定时设置
//            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job.getCronExpression());
//
//            // 按新的cronExpression表达式重新构建trigger
//            trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
//            // 按新的trigger重新设置job执行
//            scheduler.rescheduleJob(triggerKey, trigger);
//        }
//    }
//
//
//
//    /**
//     * 恢复一个job
//     *
//     * @param scheduleJob
//     * @throws SchedulerException
//     */
//    public void resumeJob(CronJob scheduleJob) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//        scheduler.resumeJob(jobKey);
//    }
//
//    /**
//     * 删除一个job
//     *
//     * @param scheduleJob
//     * @throws SchedulerException
//     */
//    public void deleteJob(CronJob scheduleJob) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//        scheduler.deleteJob(jobKey);
//
//    }
//
//    /**
//     * 立即执行job
//     *
//     * @param scheduleJob
//     * @throws SchedulerException
//     */
//    public void runAJobNow(CronJob scheduleJob) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        JobKey jobKey = JobKey.jobKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//        scheduler.triggerJob(jobKey);
//    }
//
//    /**
//     * 更新job时间表达式
//     *
//     * @param scheduleJob
//     * @throws SchedulerException
//     */
//    public void updateJobCron(CronJob scheduleJob) throws SchedulerException {
//        Scheduler scheduler = schedulerFactoryBean.getScheduler();
//        TriggerKey triggerKey = TriggerKey.triggerKey(scheduleJob.getJobName(), scheduleJob.getJobGroup());
//
//        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
//
//        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression());
//
//        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
//
//        scheduler.rescheduleJob(triggerKey, trigger);
//    }
//
//    /**
//     * 从数据库中查询job
//     */
//    public CronJob getTaskById(Long jobId) {
//        return jobDao.selectByPrimaryKey(jobId);
//    }
//
//
//}
